package com.ruoyi.yipaiju.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.enums.dict.DiligenceReportOrderStatus;
import com.ruoyi.common.utils.bean.Builder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yipaiju.domain.DiligenceReportOrder;
import com.ruoyi.yipaiju.service.IDiligenceReportOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 尽调报告订单Controller
 * 
 * @author liuwp
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/yipaiju/diligence_report_order")
public class DiligenceReportOrderController extends BaseController
{
    @Autowired
    private IDiligenceReportOrderService diligenceReportOrderService;

    /**
     * 查询尽调报告订单列表
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:diligence_report_order:list')")
    @GetMapping("/list")
    public TableDataInfo list(DiligenceReportOrder diligenceReportOrder)
    {
        startPage();
        List<DiligenceReportOrder> list = diligenceReportOrderService.selectDiligenceReportOrderList(diligenceReportOrder);
        return getDataTable(list);
    }

    /**
     * 导出尽调报告订单列表
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:diligence_report_order:export')")
    @Log(title = "尽调报告订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DiligenceReportOrder diligenceReportOrder)
    {
        List<DiligenceReportOrder> list = diligenceReportOrderService.selectDiligenceReportOrderList(diligenceReportOrder);
        ExcelUtil<DiligenceReportOrder> util = new ExcelUtil<DiligenceReportOrder>(DiligenceReportOrder.class);
        util.exportExcel(response, list, "尽调报告订单数据");
    }

    /**
     * 获取尽调报告订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:diligence_report_order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(diligenceReportOrderService.selectDiligenceReportOrderById(id));
    }

    /**
     * 新增尽调报告订单
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:diligence_report_order:add')")
    @Log(title = "尽调报告订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DiligenceReportOrder diligenceReportOrder)
    {
        return toAjax(diligenceReportOrderService.insertDiligenceReportOrder(diligenceReportOrder));
    }

    /**
     * 修改尽调报告订单
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:diligence_report_order:edit')")
    @Log(title = "尽调报告订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DiligenceReportOrder diligenceReportOrder)
    {
        return toAjax(diligenceReportOrderService.updateDiligenceReportOrder(diligenceReportOrder));
    }

    /**
     * 删除尽调报告订单
     */
    @PreAuthorize("@ss.hasPermi('yipaiju:diligence_report_order:remove')")
    @Log(title = "尽调报告订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(diligenceReportOrderService.deleteDiligenceReportOrderByIds(ids));
    }

    /**
     * 手动生成报告
     * @param id 订单号id
     * @return
     */
    @PostMapping("/manuall-generate-report/{id}")
    public AjaxResult manuallGenerateReport(@PathVariable("id") Long id){
        try {
            // 处理业务逻辑
            DiligenceReportOrder diligenceReportOrder = diligenceReportOrderService.selectDiligenceReportOrderById(id);
//            diligenceReportOrder.setStatus(DiligenceReportOrderStatus.SUCCESS.getValue());
//            diligenceReportOrder.setTransactionId(diligenceReportOrder.getTransactionId());
//            diligenceReportOrderService.updateDiligenceReportOrder(diligenceReportOrder);
            diligenceReportOrderService.sendTeplateMessage(diligenceReportOrder, "正在为您生成报告，请稍后...");
            new Thread(() -> {
                // TODO: 根据支付结果更新订单状态等业务逻辑
                if (diligenceReportOrder != null) {
                    boolean pdf = diligenceReportOrderService.createPdf(diligenceReportOrder);
                    if (pdf){
                        // TODO 发消息
                        diligenceReportOrderService.sendTeplateMessage(diligenceReportOrder, "您的尽调报告已生成，详情请点击查看");
                        diligenceReportOrder.setStatus(DiligenceReportOrderStatus.RESUCESS.getValue());

                    }else {
                        diligenceReportOrder.setStatus(DiligenceReportOrderStatus.REFAIL.getValue());
                    }
                }
                diligenceReportOrderService.updateDiligenceReportOrder(diligenceReportOrder);
            }).start();
            // 返回成功响应
            return AjaxResult.success();
        } catch (Exception e) {
            // 返回失败响应
            return AjaxResult.error();
        }
    }

    /**
     * 手动发送消息
     * @param id 订单号id
     * @return
     */
    @PostMapping("/manuall-send-message/{id}")
    public AjaxResult manuallSendMessage(@PathVariable("id") Long id) {
        try {
            // 处理业务逻辑
            DiligenceReportOrder diligenceReportOrder = diligenceReportOrderService.selectDiligenceReportOrderById(id);
            // TODO 发消息
            diligenceReportOrderService.sendTeplateMessage(diligenceReportOrder, "您的尽调报告已生成，详情请点击查看");
            // 返回成功响应
            return AjaxResult.success();
        } catch (Exception e) {
            // 返回失败响应
            return AjaxResult.error();
        }
    }


}
